package core.lexical;

import java.io.FileReader;

import core.LexicalToken;
import core.Token;
import core.error.ErrorHandler;

/**
 * It does the analysis of the source code splitting the text into tokens according to 
 * FORD rules
 * @author Ricardo Ara�jo Santos
 * @author Felipe Leal Coutinho
 * 
 * @version Last revised by Ricardo at 27/01/08
 */
public class LexicalAnalyser {
	
	/**
	 * The LexicalClassificator classifies each token into FORD language symbols. 
	 */
	private LexicalClassificator classificator;
	
	/**
	 * The LexicalTokenizer split the source code text into {@link Token} objects.
	 */
	private LexicalTokenizer tokenizer;

	private final ErrorHandler errorHandler;
	
	
	/**
	 * Constructs LexicalAnalyzer using the given file reader and error handler.
	 * @param sourceReader the file reader.
	 * @param errorHandler the error handler.
	 */
	public LexicalAnalyser( FileReader sourceReader, ErrorHandler errorHandler ) {
		this.errorHandler = errorHandler;
		tokenizer = new LexicalTokenizer( sourceReader );
		classificator = new LexicalClassificator();
	}
	
	/**
	 * Returns the next token in the source code.
	 * @return the next LexicalToken
	 */
	public LexicalToken nextToken(){
		return classificator.classify( tokenizer.nextToken() );
	}
	
	public int getLineNumber() {
		return tokenizer.getLineNumber();
	}

	public ErrorHandler getErrorHandler() {
		return errorHandler;
	}
}

